package com.foreknow.service;

import com.foreknow.bean.User;
import com.foreknow.dao.UserDao;
import com.foreknow.dao.impl.UserDaoImpl;
import com.foreknow.db.DBManager;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {

    @Override
    public boolean addUser(User user) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        UserDaoImpl userDao = new UserDaoImpl();
        userDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = userDao.save(user);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

//    public static void main(String[] args) {
//        User user = new User();
//        user.setAdminName("QQQQQ");
//        user.setPassword("8888888");
//        UserService userService = new UserServiceImpl();
//        boolean isRight = userService.addUser(user);
//        System.out.println(isRight);
//    }
    @Override
    public User isValidate(String name, String password) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        UserDaoImpl userDao = new UserDaoImpl();
        userDao.setConnection(conn);
        User user = null;
        try {
            user = userDao.login(name, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    public static void main(String[] args) {
        UserService userService = new UserServiceImpl();
        User user = userService.isValidate("lisi","123");
        System.out.println(user.getAdminID()+" "+user.getAdminName());
    }
}
